<template>
  <div style="padding: 6px;">
    <el-form ref="searchForm" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="14">
          <el-form-item label="Name">
            <el-input v-model="sd.searchName" size="small" placeholder="Name"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-button class="mt_5" type="primary" size="mini" icon="el-icon-search" @click="searchUser">search</el-button>
          <el-button class="mt_5" type="primary" size="mini" icon="el-icon-plus" @click="addUser">add</el-button>
        </el-col>
      </el-row>
    </el-form>

    <el-table :data="userObj.list" border style="width: 100%">
      <el-table-column label="Name" prop="name"></el-table-column>
      <el-table-column label="Phone" prop="phone"></el-table-column>
      <el-table-column label="Account" prop="username"></el-table-column>
      <el-table-column label="#" width="150px">
        <template v-slot="scope">
          <el-button size="mini" type="primary" @click="editUser(scope.row.id)">edit</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination v-if="userObj.total>sd.pageSize" class="center" background layout="total, prev, pager, next"
                   @current-change="loadUser" :total="userObj.total"
                   :current-page.sync="sd.pageNum" :page-size="sd.pageSize">
    </el-pagination>

    <el-dialog :title="popupTitle" :visible.sync="entityDetailVisible" width="800px">
      <keHuDetail v-if="entityDetailVisible" :visible.sync="entityDetailVisible" :entityId="popupEntityId"></keHuDetail>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request'
import keHuDetail from './keHuDetail'
export default {
  components: {keHuDetail},
  data() {
    return {
      sd: {
        searchName : '',
        pageNum: 1,
        pageSize: 5
      },
      userObj: {},

      entityDetailVisible : false,
      popupTitle : '',
      popupEntityId : 0,

      userRoleVisible : false,
    }
  },
  mounted() {
    this.loadUser()
    this.$EventBus.$on("kehuadd", () => {
      this.sd.pageNum = 1;
      this.loadUser();
    });
    this.$EventBus.$on("kehuedit", () => {
      this.loadUser();
    });
  },
  beforeDestroy() {
    this.$EventBus.$off("kehuadd")
    this.$EventBus.$off("kehuedit")
  },
  methods: {
    searchUser(){
      this.sd.pageNum = 1;
      this.loadUser();
    },
    loadUser() {
      request({
        url: '/emp/kehu/pagekehu',
        data: this.sd
      }).then(res => {
        const { data } = res
        this.userObj = data
      })
    },
    addUser(){
      this.popupEntityId = 0;
      this.popupTitle= "add custom"
      this.entityDetailVisible= true
    },
    editUser(id){
      this.popupEntityId= id;
      this.popupTitle= "edit custom"
      this.entityDetailVisible= true
    }
  }
}
</script>
